package cn.edu.scnu.semart.message.controller;

import cn.edu.scnu.semart.model.chat.ChatMessage;
import cn.edu.scnu.semart.message.mapper.ChatMessageMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatHistoryController {
    @Autowired
    private ChatMessageMapper chatMessageMapper;

    //查询历史消息
    @GetMapping("/chat/history")
    public IPage<ChatMessage> getChatHistory(@RequestParam Long conversationId,
                                             @RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "20") int size) {
        Page<ChatMessage> pageObj = new Page<>(page, size);
        QueryWrapper<ChatMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("conversation_id", conversationId).orderByAsc("send_at");
        return chatMessageMapper.selectPage(pageObj, wrapper);
    }
} 